<?php
session_start();

include 'inc/db_connect.php';
include 'inc/db_op.php';
include 'inc/html_op.php';
include 'inc/form_op.php';

$wlog_fields = Array("task_id", "updated_progress", "end_time", "title", 
    "content", "work_hours", "overtime_hours", "health", "user_id");
    
$wlog_update_fields = array("end_time", "title", "content", "work_hours", "overtime_hours", "health",
  "updated_progress");
    
$delete_wlog_post_name = "delete_wlog_id";


function gen_task_select() {
  $select_options = array();
  $user_id = $_SESSION['user_id'];
  $rows = db_get("select id, title from task_info where assignto = $user_id and progress < 100");
  
  foreach($rows as $row) {
    $select_options[$row['id']] = $row['title'];
  }
  $select_options[null] = "学习、思考、其他...";
  return gen_select("task_id", $select_options, isset($_POST['new_log_task_id']) ? $_POST['new_log_task_id'] : -1)."<a href=task.php target=_blank>新建任务</a>";
}

function gen_own_log_list() {
  $head = "<h2>本人最近工作</h2>";
  $common_fields = " w.id wid, date_format(end_time, '%m-%d %a') end_time, w.title wtitle, w.content content, 
    updated_progress, work_hours, overtime_hours, w.last_modified last_modified";
  $where = " where user_id = ".$_SESSION['user_id'];
  $sql = "select t.title ttitle, $common_fields from task_info t join work_log w on t.id = w.task_id $where
    union
    select '', $common_fields from work_log w $where and task_id = 0 order by last_modified desc limit 12";

  $rows = db_get($sql);
  global $delete_wlog_post_name;
  foreach($rows as $row) {
    $del_code = 'javascript:';
    $del_code .= 'confirm("真的要删除吗？")&&submit_a_value("'.$delete_wlog_post_name.'", "'.$row['wid'].'")';
    $del_link = "<a href='".$del_code."'>删除</a>";
    $modify_code = 'javascript:get_wlog_modify_form("'.$row['wid'].'", "wlog_modify_form", "own_wlog");';
    $modify_code .= 'switch_disp("wlog_modify_form","own_wlog");';
    $modify_link = "<a href='".$modify_code."'>修改</a>";
    $disp_str = gen_sep_str(array($row['end_time'],
      $row['ttitle'],$row['wtitle'],$row['updated_progress']."%完成"), '，')
        ."。$del_link $modify_link$modify_form_div";
    $list_items[$disp_str] = $row['content'];
  }
  $href_code = 'javascript:switch_disp("wlog_form", "own_wlog");';
  $add_wlog_link = "<a href='$href_code';>添加记录</a>";

  return $head.gen_list_with_tip($list_items).$add_wlog_link;
}

function gen_progress_select() {
  $select_options = array();
  for($i = 10; $i <= 100; $i += 10) {
    $select_options[$i] = "$i%";
  }
  return gen_select("updated_progress", $select_options, "50");
}

function gen_colleague_log_list($department_only = 1) {
  $common_fields = "name, date_format(end_time, '%m-%d %a') end_time, w.title wtitle, w.content content, 
    updated_progress, work_hours, overtime_hours, w.last_modified last_modified";
  $where = " where user_id != ".$_SESSION['user_id'];
  if($department_only) {
    $where .= " and user_id in (select id from user_info where department_id = ". $_SESSION['department_id'].") ";
  }
  $sql = "select t.title ttitle, $common_fields from (user_info u, task_info t) join work_log w 
    on u.id=w.user_id and t.id = w.task_id $where
    union
    select '', $common_fields from user_info u join work_log w on u.id = w.user_id $where and task_id = 0
    order by last_modified desc limit 32";
    
  $rows = db_get($sql);
  foreach($rows as $row) {
    $disp_str = gen_sep_str(array(str_replace(' ', '&nbsp', $row['name']),$row['end_time'],
       $row['ttitle'],$row['wtitle'],$row['updated_progress']."%完成"), '，');
    $list_items[$disp_str] = $row['content'];
  }
  return gen_list_with_tip($list_items);
}

function get_left_content() {
  $wlog_form_rows = array(
  "日期"=>gen_select("end_time", 
    array(date('Y-m-d')=>"今天",
          date('Y-m-d', strtotime("-1 day"))=>"昨天", 
          date('Y-m-d', strtotime("-2 day"))=>"前天" ,
		  date('Y-m-d', strtotime("-3 day"))=>"大前天" )),
  "任务"=>gen_task_select(),
  "标题"=>"<input type='text' require name='title' size=40 required />",
  "内容"=>"<textarea rows=12 cols=50 required name='content'></textarea>",
  "进度变为"=>gen_progress_select(),
  "耗时"=>"<input type='number' name='work_hours' size=4 min=0.0 max=24 step=0.25 required/>小时",
  "其中加班"=>"<input type='number' name='overtime_hours' size=4  min=0.0 max=24.0 step=0.25 />小时",
  "身体情况"=>gen_select("health",
    array("健康"=>"健康", "良好"=>"良好", "疲劳"=>"疲劳", "不适"=>"不适", "生病"=>"生病")),
    ""=>"<button name='new_wlog_submit' type='submit'>提交</button>
         <a href='javascript:switch_disp(".'"own_wlog", "wlog_form"'.");'>取消</a>"
  );

  $wlog_modify_rows = array(
  "日期"=>"<input type=date name=end_time/>",
  "标题"=>"<input type=text name=title required/>",
  "内容"=>"<textarea rows=12 cols=50 name=content required></textarea>",
  "进度"=>"<input type=number name=updated_progress required/>",
  "耗时"=>"<input type=number name=work_hour required />",
  ""=>"<button name='wlog_modify_submit' type='submit'>提交</button>
       <a href='javascript:switch_disp(".'"own_wlog", "wlog_modify_form"'.");'>取消</a>");


  $own_wlog = gen_own_log_list();

  $wlog_form = "<h2>添加工作记录</h2>
  <form method='POST' name='work_log' id='work_log'>
  <input type=hidden name='user_id' value=".$_SESSION['user_id']." />"
  .gen_twincol_table("wlog", $wlog_form_rows)
  ."</form>";

  $wlog_modify_form = "";

  $divs = array("own_wlog"=>$own_wlog, "wlog_form"=>$wlog_form, "wlog_modify_form"=>$wlog_modify_form);
  return gen_switch_divs($divs, isset($_POST['new_log_task_id']) ? "wlog_form" : "own_wlog");
}

$left_content = get_left_content();

$department_colleague_log_list = "<h2>部门同事最近工作 <a href=".'javascript:switch_disp("all_clist","dept_clist");'.">查看所有</a></h2>".gen_colleague_log_list(1);
$all_colleague_log_list = "<h2>所有同事最近工作 <a href=".'javascript:switch_disp("dept_clist","all_clist");'.">只看部门</a></h2>".gen_colleague_log_list(0);
$right_content = gen_switch_divs(array("dept_clist"=>$department_colleague_log_list, "all_clist"=>$all_colleague_log_list), "dept_clist");

perform_insert_post('new_wlog_submit', 'work_log', $wlog_fields);
perform_delete_post($delete_wlog_post_name, 'work_log');
perform_update_post('wlog_modify_submit', 'work_log', $wlog_update_fields);

$debug = 0;
echo gen_twincol_page("Wlog", $left_content, $right_content, $debug);
?>

<?php
//$ip = $_SERVER['REMOTE_ADDR'];
//echo $ip;
//$geoInfo = get_meta_tags("http://www.geobytes.com/IpLocator.htm?GetLocation&template=php3.txt&IpAddress=$ip");
//echo $geoInfo["locationcode"];
?>